package org.abratuhi.kvngroupware.network;

import java.net.Socket;

public class S_Client extends AbstractClient{
	
	private Server server;
	
	public S_Client(Socket socket, MessageProceeder mproceeder, Server server){
		super(socket, mproceeder);
		this.server = server;
		this.setRunOK(true);
	}
	
	public void register(){
		server.register(this);
	}
	
	public void unregister(){
		server.unregister(this);
	}
	
	public void sendString(String str){
		super.sendString(str);
		System.out.println("S_CLIENT [send]: "+str); //TODO: logger
	}
	
	public String receiveString(){
		String str = super.receiveString();
		System.out.println("S_CLIENT [recv]: "+str); // TODO: logger
		return str;
	}
	
	public void run(){
		while(getRunOK()){
			try {
				Message message = receiveMessage();
				if(message!=null) mproceeder.proceedMessage(this, message);
				Thread.sleep(100);
			} catch (InterruptedException e) {
				System.out.println("S_CLIENT [interrupt]");
				break;
			}
		}
		System.out.println("S_CLIENT [stop]");
	}

	@Override
	public void doOnCommunicationError() {
		stop();
		server.clients.remove(getId());
	}

}

